package com.jwgf.mqtt.json;

import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.badoo.mobile.util.WeakHandler;
import com.jwgf.Half485.modbusthreadpool.PLCData;
import com.jwgf.common.AppContext;
import com.jwgf.common.IPConfig;
import com.jwgf.common.MD5;
import com.jwgf.common.ScreenOffTimeoutControl;
import com.jwgf.common.Utility;
import com.jwgf.fram.FramManager;
import com.jwgf.group.Group;
import com.jwgf.group.GroupMap;
import com.jwgf.mqtt.MqttClientManager;
import com.jwgf.objectbox.CbfReport;
import com.jwgf.objectbox.FaultRecord;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.ProcessRecord;
import com.jwgf.objectbox.RedLightRecord;
import com.jwgf.objectbox.ShiftReport;
import com.jwgf.objectbox.SpindleParameter;
import com.jwgf.objectbox.StatisticRecord;
import com.jwgf.objectbox.TubeRecord4E;
import com.jwgf.report.ShiftTimeRule;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.Report;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineModBusReadParser;
import com.jwgf.smarowindingmachine_unity.logic.UnitUtils;
import com.jwgf.smarowindingmachine_unity.ui.SmaroWindingMachineActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import io.objectbox.Box;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

public class JsonBuilder {
    private static Disposable disposable;
    private static JSONObject machineParameter = new JSONObject();
    private static JSONObject groupParameter = new JSONObject();
    private static JSONObject report = new JSONObject();

    public static JSONObject buildMachineParameter(String key) {
        MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

        try {
            machineParameter.clear();
            machineParameter.put(key, "持续变化的数据！");
            machineParameter.put("ip", IPConfig.getIp());
            machineParameter.put("id", IPConfig.getMachineID());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日HH:mm:ss", Locale.US);
            machineParameter.put("机台时间", sdf.format(new Date()));
            machineParameter.put("数据组", "整车数据");
            machineParameter.put("机器编号", sp.getValue(MachineParameterSharedPreference.SYSTEM_MACHINE_NUM, "1"));
            machineParameter.put("员工号", "");
            machineParameter.put("机器序列号", sp.getValue(MachineParameterSharedPreference.SYSTEM_SERIAL_NUM, "1"));
            machineParameter.put("屏保时间", ScreenOffTimeoutControl.getTimeout());
            String language = sp.getValue(MachineParameterSharedPreference.SYSTEM_LANGUAGE, "0").equals("0") ? "中文" : "英文";
            machineParameter.put("语言", language);
            String driveType = sp.getValue(MachineParameterSharedPreference.SYSTEM_MACHINE_DRIVE, "0").equals("0") ? "左手车" : "右手车";
            machineParameter.put("左右车型", driveType);
            String lengthUnit = sp.getValue(MachineParameterSharedPreference.SYSTEM_LENGTH_UNIT, "0").equals("0") ? "米" : "码";
            machineParameter.put("长度单位", lengthUnit);
            String weightUnit = sp.getValue(MachineParameterSharedPreference.SYSTEM_WEIGHT_UNIT, "0").equals("0") ? "千克" : "磅";
            machineParameter.put("重量单位", weightUnit);
            String countUnit = null;
            if (sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT, "0").equals("0")) {
                countUnit = "英支";
            } else if (sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT, "0").equals("1")) {
                countUnit = "公支";
            } else {
                countUnit = "号";
            }
            machineParameter.put("支数单位", countUnit);
            String yarnCleaner = null;
            if (sp.getValue(MachineParameterSharedPreference.SYSTEM_YARN_CLEANER, "0").equals("0")) {
                yarnCleaner = "USTER";
            } else if (sp.getValue(MachineParameterSharedPreference.SYSTEM_YARN_CLEANER, "0").equals("1")) {
                yarnCleaner = "keisokki";
            } else if (sp.getValue(MachineParameterSharedPreference.SYSTEM_YARN_CLEANER, "0").equals("2")) {
                yarnCleaner = "Loepfe";
            } else {
                yarnCleaner = "长岭";
            }
            machineParameter.put("电子清纱器型号", yarnCleaner);
            String machineType = null;
            if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("0")) {
                machineType = "人工落纱";
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("1")) {
                machineType = "自动落纱";
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("2")) {
                machineType = "自动喂管";
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("3")) {
                machineType = "细络联";
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("4")) {
                machineType = "筒倒筒";
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("5")) {
                machineType = "大纱库";
            }
            machineParameter.put("机器类型", machineType);
            machineParameter.put("负压传感器负压值", sp.getValue(MachineParameterSharedPreference.MACHINE_MINUS_PRESSURE, "45"));
            machineParameter.put("清洁风机间歇时间", sp.getValue(MachineParameterSharedPreference.MACHINE_INTERVAL_TIME, "0"));
            machineParameter.put("清洁风机巡回次数", sp.getValue(MachineParameterSharedPreference.MACHINE_CYCLE_NUM, "0"));
            String transport = null;
            if (sp.getValue(MachineParameterSharedPreference.MACHINE_TRANSPORT, "0").equals("0")) {
                transport = "机头";
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_TRANSPORT, "0").equals("1")) {
                transport = "机尾";
            } else {
                transport = "无";
            }
            machineParameter.put("筒纱输送带类型", transport);
            machineParameter.put("单锭总数", sp.getValue(0x29, 1));
            int grpCount = GroupMap.getInstance().getGroupCount();
            machineParameter.put("分组总数", String.valueOf(GroupMap.getInstance().getGroupCount()));
            JSONArray grpSetting = new JSONArray();
            for (int grp = 1; grp <= grpCount; ++grp) {
                List<Integer> spindleList = GroupMap.getInstance().getSpindleList(grp);
                if (spindleList.size() > 0) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("组号", grp);
                    jsonObject.put("首单锭号", spindleList.get(0));
                    jsonObject.put("末单锭号", spindleList.get(spindleList.size() - 1));
                    grpSetting.add(grp - 1, jsonObject);
                }
            }
            machineParameter.put("分组设置", grpSetting);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return machineParameter;
    }

    public static JSONObject buildGroupParameter() {
        try {
            groupParameter.clear();
            groupParameter.put("ip", IPConfig.getIp());
            groupParameter.put("id", IPConfig.getMachineID());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日HH:mm:ss");
            groupParameter.put("机台时间", sdf.format(new Date()));
            groupParameter.put("数据组", "分组设置数据");

            List<Group> groups = GroupMap.getInstance().getGroupList();
            JSONArray grpSetting = new JSONArray();
            Box<SpindleParameter> box = ObjectBox.get().boxFor(SpindleParameter.class);
            for (int grp = 0; grp < groups.size(); ++grp) {
                long id = groups.get(grp).getId();
                if (id == 0) break;
                SpindleParameter para = box.get(id);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("组号", grp + 1);
                jsonObject.put("首单锭号", groups.get(grp).getStartSpindle());
                jsonObject.put("末单锭号", groups.get(grp).getEndSpindle());
                jsonObject.put("络纱速度", para.frame20DB);
                jsonObject.put("纱线支数", para.frame28DC);
                jsonObject.put("纱线股数", para.endsNum);
                jsonObject.put("筒纱长度", UnitUtils.getUILength(para.frame24DB));
                jsonObject.put("筒纱重量", UnitUtils.getUIWeightFromLength(Integer.valueOf(para.frame24DB), para));
                jsonObject.put("管纱长度", UnitUtils.getUILength(para.frame24DC));
                jsonObject.put("管纱重量", UnitUtils.getUIWeightFromLength(Integer.valueOf(para.frame24DC), para));
                jsonObject.put("滑移系数", para.slipCoefficient);
                jsonObject.put("产量修正系数", para.productionCorrectFactor);
                jsonObject.put("筒纱最大接头数", para.frame25DB);
                jsonObject.put("管纱最大剪切数", para.frame25DC);
                jsonObject.put("新筒管起始卷绕速度", para.frame28DB);
                jsonObject.put("启动加速时间(大)", para.frame10DB);
                jsonObject.put("启动加速时间(中)", para.frame20DC);
                jsonObject.put("启动加速时间(小)", para.frame10DC);
                jsonObject.put("慢制动减速时间", para.frame28DD);
                jsonObject.put("短疵倒转速度", UnitUtils.getUILength(para.frame21DB));
                jsonObject.put("长疵倒转速度", UnitUtils.getUILength(para.frame21DC));
                jsonObject.put("换管次数", para.frame22DD);
                jsonObject.put("接头循环次数", para.frame22DB);
                jsonObject.put("筒纱吸头次数", para.frame22DC);
                jsonObject.put("张力值", para.frame27DCH);
                jsonObject.put("张力上限百分率", para.frame27DDL);
                jsonObject.put("张力下限百分率", para.frame27DDH);
                jsonObject.put("自动调速启动最小压力", para.frame27DBH);
                jsonObject.put("自动调速启动管纱长度", para.frame26DB);
                jsonObject.put("速度减小百分率", para.frame26DC);
                jsonObject.put("启动增压", para.frame27DCL);
                jsonObject.put("防叠周期", para.frame23DB);
                jsonObject.put("加速时间百分比", para.frame23DCL);
                jsonObject.put("速度增加百分比", para.frame23DCH);
                jsonObject.put("非重叠区周期增加百分比", para.frame2ADC);
                jsonObject.put("重叠区速度增加百分比", para.frame2ADB);
                jsonObject.put("吹风时间", para.frame25DD);
                jsonObject.put("单纱强力", para.frame2EDC);
                jsonObject.put("张力盘速度", para.frame2EDB);
                jsonObject.put("张力传感器", para.frame27DBL);
                boolean vss = Utility.BitUtils.getBitValue(Integer.valueOf(para.frame15DC), 2);
                jsonObject.put("自动调速", vss ? "1" : "0");
                int sense = Integer.parseInt(para.frame26DD);
                String antiWrapSense = "";
                switch (sense) {
                    case 1:
                        antiWrapSense = "0";
                        break;
                    case 2:
                        antiWrapSense = "10";
                        break;
                    case 4:
                        antiWrapSense = "20";
                        break;
                    case 8:
                        antiWrapSense = "30";
                        break;
                    case 16:
                        antiWrapSense = "40";
                        break;
                    case 32:
                        antiWrapSense = "50";
                        break;
                    case 64:
                        antiWrapSense = "70";
                        break;
                    default:
                        break;
                }
                jsonObject.put("防缠绕灵敏度", antiWrapSense);
                jsonObject.put("采样管纱上限百分率", para.sampleCopUplimit);
                jsonObject.put("采样管纱下限百分率", para.sampleCopLowlimit);
                jsonObject.put("采样管纱数", para.sampleCopCount);
                jsonObject.put("张力传感器与纱线单向接触次数", para.sensorYarnTouchTimes);
                int mode = Integer.parseInt(para.frame23DD);
                String antiMode = "";
                if (mode == 0) {
                    antiMode = "普通型";
                } else {
                    antiMode = "加强型";
                }
                jsonObject.put("防叠方式", antiMode);
                jsonObject.put("筒纱直径比", para.frame24DD);
                jsonObject.put("捻接失败自启动次数", para.frame21DDH);
                jsonObject.put("换管失败自启动次数", para.frame21DDL);
                grpSetting.add(grp, jsonObject);
            }
            groupParameter.put("分组设置数据", grpSetting);


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return groupParameter;
    }

    public static JSONObject buildReport() {
        MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

        try {
            report.clear();
            report.put("序列号", MD5.readLine("//data//serial"));
            report.put("ip", IPConfig.getIp());
            report.put("id", IPConfig.getMachineID());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
            report.put("机台时间", sdf.format(new Date()));
            report.put("数据组", "周期更新");
            report.put("当前班次号", ShiftTimeRule.getInstance().getCurrentShiftNum());
            report.put("班组",ShiftTimeRule.getInstance().getShiftTeam());
            ShiftReport sum = Report.getInstance().getTailSumProduction();
            report.put("整车产量(kg)", String.valueOf(sum.production));
            report.put("整车效率(%)", String.valueOf(sum.runEfficiency));
            CbfReport cbfReport = Report.getInstance().getTailCBFDoffer();
            if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("5")) {
                report.put("进管数(个)", String.valueOf(cbfReport.enterTubeCountD));
                report.put("空管回送数(个)", String.valueOf(cbfReport.emptyTubeCountD));
                report.put("残纱数量(个)", String.valueOf(cbfReport.remnantTubeCountD));
                report.put("进入单锭数(个)", String.valueOf(cbfReport.enterSpindleCountD));
                report.put("第一生头通过数(个)", String.valueOf(cbfReport.joint1PassCountD));
                report.put("第一生头成功数(个)", String.valueOf(cbfReport.joint1SucceedCountD));
                report.put("第二生头通过数(个)", String.valueOf(cbfReport.joint2PassCountD));
                report.put("第二生头成功数(个)", String.valueOf(cbfReport.joint2SucceedCountD));
                report.put("拔管处通过数(个)", String.valueOf(cbfReport.pullTubeCount));
                report.put("第一生头成功率(%)", String.valueOf(cbfReport.joint1SucceedRatioD));
                report.put("第二生头成功率(%)", String.valueOf(cbfReport.joint2SucceedRatioD));
                report.put("残纱率(%)", String.valueOf(cbfReport.remnantTubeRatioD));
                report.put("空管率(%)", String.valueOf(cbfReport.emptyTubeRatioD));
            } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("3")) {
                report.put("进管数(个)", String.valueOf(cbfReport.enterTubeCountX));
                report.put("空管回送数(个)", String.valueOf(cbfReport.emptyTubeCountX));
                report.put("残纱数量(个)", String.valueOf(cbfReport.remnantTubeCountX));
                report.put("一通道生头成功数(个)", String.valueOf(cbfReport.channel1SucceedCountX));
                report.put("一通道生头不成功数(个)", String.valueOf(cbfReport.channel1FailedCountX));
                report.put("二通道生头成功数(个)", String.valueOf(cbfReport.channel2SucceedCountX));
                report.put("二通道生头不成功数(个)", String.valueOf(cbfReport.channel2FailedCountX));
                report.put("一通道生头成功率(%)", String.valueOf(cbfReport.channel1SucceedRatioX));
                report.put("二通道生头成功率(%)", String.valueOf(cbfReport.channel2SucceedRatioX));
                report.put("残纱率(%)", String.valueOf(cbfReport.remnantTubeRatioX));
                report.put("空管率(%)", String.valueOf(cbfReport.emptyTubeRatioX));
            }
            report.put("小车落筒成功个数(个)", String.valueOf(cbfReport.dofferDoffSucceedCount));
            report.put("小车落筒个数(个)", String.valueOf(cbfReport.dofferDoffCount));
            report.put("小车落筒成功百分率(%)", String.valueOf(cbfReport.dofferSucceedRatio));
            report.put("总满筒个数(个)", String.valueOf(cbfReport.doffCount));
            report.put("总落筒成功百分率(%)", String.valueOf(cbfReport.doffSucceedRatio));
            if (SmaroWindingMachineActivity.canHandStack != null) {
                report.put("CAN成功数", SmaroWindingMachineActivity.canHandStack.succeedCount());
                report.put("CAN总数", SmaroWindingMachineActivity.canHandStack.totalCount());
            }
            report.put("HMI软件版本", AppContext.version);
            if (Build.VERSION.RELEASE.equals("6.0.1")) {
                report.put("HMI硬件", "15寸");
            } else {
                report.put("HMI硬件", "10寸");
            }

            List<ShiftReport> groupList = Report.getInstance().getTailGroupProduction();
            JSONArray groupArray = new JSONArray();
            for (int i = 0; i < groupList.size(); ++i) {
                JSONObject group = new JSONObject();
                group.put("组号", i + 1);
                group.put("组产量(kg)", String.valueOf(groupList.get(i).production));
                group.put("组效率(%)", String.valueOf(groupList.get(i).runEfficiency));
                groupArray.add(i, group);
            }
            report.put("分组数据", groupArray);
            if (PLCData.getInstance() != null) {
                if (SmaroWindingMachineModBusReadParser.getInstance()
                        .getCurrentFaultList().isEmpty()) {
                    report.put("工作状态", "3001");
                } else {
                    report.put("工作状态", "3002");
                }
                if (PLCData.getInstance().getHoldingRegister(1, 253) != 0) {
                    report.put("设备状态", "2001"); //运行
                } else {
                    report.put("设备状态", "2002"); //停止
                }
                report.put("整车报警", PLCData.getInstance().getHoldingRegister(1, 252));
                report.put("直流电源报警", PLCData.getInstance().getHoldingRegister(1, 254));
                report.put("细络联报警", PLCData.getInstance().getHoldingRegister(1, 307));
                report.put("大纱库报警1", PLCData.getInstance().getHoldingRegister(1, 331));
                report.put("大纱库报警2", PLCData.getInstance().getHoldingRegister(1, 332));
                report.put("纱库盘报警", PLCData.getInstance().getHoldingRegister(1, 352));
            } else {
                report.put("设备状态", "0");
                report.put("工作状态", "0");
            }
            final List<TubeRecord4E> data = Report.getInstance().getTubeMonitor4E();
            JSONArray rfid = new JSONArray();
            for (TubeRecord4E item : data) {
                JSONObject object = new JSONObject();
                int tubeNumber = item.tubeNum;
                String isLeft = (tubeNumber & 0xffff) == 1 ? "左" : "右";
                object.put("左右车", isLeft);
                object.put("管号", tubeNumber >> 16);
                Date date = new Date(item.enterTime);
                object.put("时间", sdf.format(date));
                rfid.add(object);
            }
            report.put("RFID", rfid);

            List<ShiftReport> spindleList = Report.getInstance().getTailSpindleProduction();
            List<StatisticRecord> spindleStatisticList = Report.getInstance().getTailSpindleStatistic();
            JSONArray spindleArray = new JSONArray();
            Box<SpindleParameter> box = ObjectBox.get().boxFor(SpindleParameter.class);
            for (int i = 0; i < spindleList.size(); ++i) {
                long id = GroupMap.getInstance().getSpindleParameterIdFromSpindle(i + 1);
                if (id == 0) break;
                SpindleParameter para = box.get(id);
                JSONObject spindle = new JSONObject();
                spindle.put("锭号", i + 1);
                spindle.put("单锭产量(kg)", String.valueOf(spindleList.get(i).production));
                spindle.put("单锭生产效率", String.valueOf(spindleList.get(i).runEfficiency));
                spindle.put("单锭机械效率", String.valueOf(spindleList.get(i).mechanicalEfficiency));
                spindle.put("当前筒纱重量", String.valueOf(spindleList.get(i).production));
                spindle.put("生产筒纱数", String.valueOf(spindleList.get(i).packageCount));
                spindle.put("更换管纱数", String.valueOf(spindleList.get(i).copChangeCount));
                spindle.put("接头数", String.valueOf(spindleList.get(i).yarnJointCount));
                spindle.put("接头数/10km", String.valueOf(spindleList.get(i).yarnJointCountPer10Km));
                spindle.put("切疵次数", String.valueOf(spindleList.get(i).cutCount));
                spindle.put("切疵次数/10km", String.valueOf(spindleList.get(i).cutCountPer10Km));
                spindle.put("捻接循环次数", String.valueOf(spindleList.get(i).spliceCount));
                spindle.put("切纱次数/管纱", String.valueOf(spindleList.get(i).cutCountPerCop));
                spindle.put("切纱次数/筒纱", String.valueOf(spindleList.get(i).cutCountPerPackage));
                spindle.put("接头数/筒纱", String.valueOf(spindleList.get(i).yarnJointCountPerPackage));
                spindle.put("络纱时间", Report.getInstance().subSecToTime(spindleList.get(i).windingTime));
                spindle.put("打结总数", String.format("%.2f", spindleStatisticList.get(i).spliceCount));
                spindle.put("打结总时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).spliceTime));
                spindle.put("打结平均时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).spliceAvgTime));
                spindle.put("落纱总数", String.format("%.2f", spindleStatisticList.get(i).packageCount));
                spindle.put("落纱总时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).dofferTime));
                spindle.put("落纱平均时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).dofferAvgTime));
                spindle.put("红灯总数", String.format("%.2f", spindleStatisticList.get(i).redLightCount));
                spindle.put("红灯总时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).redLightTime));
                spindle.put("红灯平均时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).redLightAvgTime));
                spindle.put("工艺报警总数", String.format("%.2f", spindleStatisticList.get(i).processCount));
                spindle.put("工艺报警总时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).processTime));
                spindle.put("工艺报警平均时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).processAvgTime));
                spindle.put("故障总数", String.format("%.2f", spindleStatisticList.get(i).faultCount));
                spindle.put("故障总时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).faultTime));
                spindle.put("故障平均时间", Report.getInstance().subSecToTime(spindleStatisticList.get(i).faultAvgTime));

                int curLengthValue = Report.getInstance().getBucketLength(i + 1);
                spindle.put("当前筒纱长度", UnitUtils.getUILength(String.valueOf(curLengthValue)));
                spindle.put("当前筒纱重量", UnitUtils.getUIWeightFromLength(curLengthValue, para));
                spindle.put("单锭小车络筒成功数", String.valueOf(spindleList.get(i).succeedDoffingPackageCount));
                spindle.put("清纱器切疵信息", FramManager.getInstance().getInt((i + 1) + "-" + 0x62 + "-" + 0, 0));
                spindle.put("清纱器报警信息", FramManager.getInstance().getInt((i + 1) + "-" + 0x63 + "-" + 0, 0));
                spindle.put("单锭报警信息1", FramManager.getInstance().getInt((i + 1) + "-" + 0x63 + "-" + 1, 0));
                spindle.put("单锭报警信息2", FramManager.getInstance().getInt((i + 1) + "-" + 0x63 + "-" + 2, 0));
                spindleArray.add(i, spindle);
            }
            report.put("机台单锭数据", spindleArray);


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return report;
    }


    public static void start() {
        disposable = Observable.interval(5, TimeUnit.MINUTES).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Throwable {
                sendReport();
            }
        });

    }

    public static void stop() {
        disposable.dispose();
    }

    public static void sendReport() {
        String jsonString = JsonBuilder.buildReport().toString();
        MqttClientManager.getInstance().publishMessage("Period", jsonString, 2);
    }
}
